package cn.com.sinosoft.testxmltransform.yewu;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;







/**
 * 机构信息服务层
 * @author xuejuan
 *
 */
@Service
@Validated
public class OrganizationServiceImpl implements OrganizationService {
    
	@Autowired
	 OrganizationMapper organizationMapper;
	
	public void setOrganizationMapper(OrganizationMapper organizationMapper) {
		this.organizationMapper = organizationMapper;
	}



	/**
	 * 批量添加机构信息
	 * 1.验证参数是否为空
	 * 2.验证数据必填项
	 * 3.保存数据
	 */
	public boolean addOrganizationList( List<OrganizationEntity> organizationlist) {
		Assert.notEmpty(organizationlist);
		for(OrganizationEntity organizationEntity:organizationlist){
		    	String primaryId = UUID.randomUUID().toString();
		    	organizationEntity.setPrimaryId(primaryId);
		 }
		//批量添加机构信息
		boolean flag = organizationMapper.addOrganizationList(organizationlist);
		
		List<DepartmentEntity> departmentList = new ArrayList<DepartmentEntity>();
	    for(OrganizationEntity organizationEntity:organizationlist){
	    	departmentList = organizationEntity.getDepartmentList();
	    	if(departmentList ==null){
	    		break;
	    	}
	    	for(DepartmentEntity departmentEntity:departmentList){
	    		departmentEntity.setOrgCode(organizationEntity.getCode());
	    		String id = UUID.randomUUID().toString();
	    		departmentEntity.setPrimaryId(id);
	    	}
	    }
	    //批量添加科室信息
	    organizationMapper.addDepartmentList(departmentList);
	    return flag;
	}

}
